package com.yx.shop.web.controller;

import com.yx.shop.common.Const;
import com.yx.shop.entitly.Address;
import com.yx.shop.entitly.User;
import com.yx.shop.service.AddressService;
import com.yx.shop.service.impl.AddressServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname AddressController
 * @Description TODO:
 * @Date 2021/05/08 20:15
 * @Created by yx
 */
@WebServlet("/address")
public class AddressController extends BaseServlet {
    private AddressService addressService = new AddressServiceImpl();

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/08 21:01
     * @Description:获取该用户所有地址
     **/
    public String getAddressList(HttpServletRequest request, HttpServletResponse response) {
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user == null) {
            request.getSession().setAttribute("msg", "添加地址必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT + "/login.jsp";
        }
        //2.访问业务层获取数据
        List<Address> addList = addressService.getAddList(user.getUid());
        request.setAttribute("addList", addList);
        return Const.FORWORD + "/self_info.jsp";
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/08 20:59
     * @Description:新增地址
     **/
    public String insertAddress(HttpServletRequest request, HttpServletResponse response) {
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user == null) {
            request.getSession().setAttribute("msg", "添加地址必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT + "/login.jsp";
        }
        //2.获取参数
        String name = request.getParameter("aname");
        String phone = request.getParameter("aphone");
        String detail = request.getParameter("adetail");

        Address address = new Address(user.getUid(), name, phone, detail);
        //3.调用业务
        addressService.insertAddress(address);

        return Const.REDIRECT + "/address?method=getAddressList";
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/09 10:27
     * @Description:根据地址id删除地址
     **/
    public String deleteAddress(HttpServletRequest request, HttpServletResponse response) {
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user == null) {
            request.getSession().setAttribute("msg", "删除地址必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT + "/login.jsp";
        }
        //2.获取参数
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //3.调用业务
        addressService.deleteAddByAid(aid);
        return Const.REDIRECT + "/address?method=getAddressList";
    }

    /**
     * @param request
     * @param response
     * @return java.lang.String
     * @Author:yx
     * @Date 2021/05/09 10:40
     * @Description:根据地址id用户id设置默认地址
     **/
    public String defaultAddress(HttpServletRequest request, HttpServletResponse response) {
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user == null) {
            request.getSession().setAttribute("msg", "设置默认地址必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT + "/login.jsp";
        }
        //2.获取参数
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //3.调用业务
        addressService.defaultAdd(aid, user.getUid());
        //设置后重新查询地址
        return Const.REDIRECT + "/address?method=getAddressList";
    }

    /**
     * @Author:yx
     * @return java.lang.String 
     * @Date 2021/05/09 10:54 
     * @param request
     * @param response
     * @Description:根据uid与aid修改地址
     **/
    public String updateAddress(HttpServletRequest request, HttpServletResponse response) {
        //1.判断是否已登录--->获取登录额session信息
        User user = (User) request.getSession().getAttribute(Const.UserInfo.LOGIN_USER);
        if (user == null) {
            request.getSession().setAttribute("msg", "修改地址必须先登录");
            //重定向至登录页面--->方便用户登录
            return Const.REDIRECT + "/login.jsp";
        }
        //2.获取参数
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        String adetail = request.getParameter("adetail");

        //访问业务层,处理修改
        Address address=new Address(user.getUid(),aname,aphone,adetail);
        address.setAid(aid);
        addressService.updateAddress(address);
        return Const.REDIRECT+"/address?method=getAddressList";
    }
}
